// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.core.property;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.dengues.commons.IDenguesSharedImage;
import org.dengues.commons.utils.ImageUtil;
import org.dengues.core.components.NodePropertyParameter;
import org.dengues.core.process.CompElement;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class PropertyTableProvider extends LabelProvider implements ITableLabelProvider, IStructuredContentProvider {

    private NodePropertyParameter param;

    private List<EParameterFieldCategory> categories;

    /**
     * Qiang.Zhang.Adolf@gmail.com PropertyTableProvider constructor comment.
     * 
     * @param propertyTableViewer
     * 
     * @param param
     */
    public PropertyTableProvider() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object,
     * java.lang.Object)
     */
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.Object, int)
     */
    public Image getColumnImage(Object element, int columnIndex) {
        Map<String, Object> map = (Map<String, Object>) element;
        if (columnIndex == 0) {
            Object object = map.get(CompElement.ITEM_KEY);
            if (object instanceof Boolean) {
                return (Boolean) object ? ImageUtil.getImage(IDenguesSharedImage.TABLEVIEW_KEY) : ImageUtil
                        .getImage(IDenguesSharedImage.TABLEVIEW_NULLKEY);
            } else {
                return null;
            }
        }
        Object object = map.get(param.getItemNames()[columnIndex - 1]);
        if (object instanceof Boolean) {
            return (Boolean) object ? ImageUtil.getImage(IDenguesSharedImage.EDITOR_CHECKBOXON) : ImageUtil
                    .getImage(IDenguesSharedImage.EDITOR_CHECKBOXOFF);
        }
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
     */
    public String getColumnText(Object element, int columnIndex) {
        if (columnIndex == 0) {
            return ""; //$NON-NLS-1$
        }
        if (element instanceof Map) {
            Map<String, Object> map = (Map<String, Object>) element;
            Object object = map.get(param.getItemNames()[columnIndex - 1]);
            if (object instanceof String) {
                return object.toString();
            }
        }
        return ""; //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
     */
    public Object[] getElements(Object inputElement) {
        if (inputElement instanceof ArrayList) {
            ArrayList<Map<String, Object>> arrayList = (ArrayList<Map<String, Object>>) inputElement;
            return arrayList.toArray(new Map[0]);
        }
        return new Object[0];
    }

    /**
     * Sets the param.
     * 
     * @param param the param to set
     */
    public void setParam(NodePropertyParameter param) {
        this.param = param;
        categories = new ArrayList<EParameterFieldCategory>();
        Object[] itemValues = param.getItemValues();
        for (int i = 0; i < itemValues.length; i++) {
            NodePropertyParameter parameter = (NodePropertyParameter) itemValues[i];
            categories.add(parameter.getFieldCategory());
        }

    }

}
